﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using MINERP.BLL.InfoConfig;
using MINERP.MODEL.InfoConfig;
using MINERP.DAL;
using MINERP.COM;
using MINERP.MODEL.FoundationModel;

namespace MINERP.InfoConfig.Controller
{
    class ProductController
    {
        #region //private fields
       
        private ProductBLL       _bll = new ProductBLL();
        private GridControl             _grid = null;
        private GridView                _view = null;
        private ComboBoxEdit            _comboBox = null;
        private GroupControl            _groupControl = null;
        private CheckEdit               _checkEdit = null;
        private List<ProductEntity>     _productEntityList = null;


        #endregion


        #region //Public Properties

        internal GroupControl GroupControl
        {
            set
            {
                _groupControl = value;
            }
        }
        internal CheckEdit CheckEdit
        {
            set
            {
                _checkEdit = value;
            }
        }
        internal ComboBoxEdit ComboBoxEdit
        {
            set
            {
                _comboBox = value;
            }
        }
        internal GridControl Grid
        {
            set
            {
                _grid = value;
            }
        }

        internal GridView View
        {
            set
            {
                this._view = value;
            }
            get
            {
                return _view;
            }
        }

        #endregion

        
        #region //Public Methods
        /// <summary>
        /// 获得单个产品对象
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public Tb_Product GetProductObj(int productId)
        {
            return _bll.GetProductObj(productId);
        }

        public Tb_Product GetProductObj(string productName)
        {
            return _bll.GetProductObj(productName);
        }
        /// <summary>
        /// 获得产品信息
        /// </summary>
        public void GetProductData()
        {
            _productEntityList = new List<ProductEntity>();
            //List<Tb_Product> products = _bll.GetProductList();
            List<Tb_Product> products;
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                products = Entities.Tb_Product.Where(p => p.Status == 1).ToList();


                foreach (Tb_Product product in products)
                {
                    if (!product.paperFaceReference.IsLoaded)
                    {
                        product.paperFaceReference.Load();
                    }
                    if (!product.paperTypeReference.IsLoaded)
                    {
                        product.paperTypeReference.Load();
                    }
                    if (!product.printerReference.IsLoaded)
                    {
                        product.printerReference.Load();
                    }
                    if (!product.Tb_PorductCategoryReference.IsLoaded)
                    {
                        product.Tb_PorductCategoryReference.Load();
                    }
                    if (!product.tb_ProductTypeReference.IsLoaded)
                    {
                        product.tb_ProductTypeReference.Load();
                    }
                    if (!product.tb_CostTypeReference.IsLoaded)
                    {
                        product.tb_CostTypeReference.Load();
                    }
                    if (!product.ProductPrintReference.IsLoaded)
                    {
                        product.ProductPrintReference.Load();
                    }
                    ProductEntity productEntity = new ProductEntity();
                    productEntity.Height = product.Height;
                    productEntity.PageNum = product.PageNum;
                    productEntity.MnemonicCode = product.mnemonicCode;
                    productEntity.PaperFaceID = product.paperFace.PaperFaceID;
                    productEntity.PaperFaceName = product.paperFace.Name;
                    productEntity.PaperTypeID = product.paperType.PaperTypeID;
                    productEntity.PaperTypeName = product.paperType.Name;
                    productEntity.PrinterName = product.printer==null?"":product.printer.PName;
                    productEntity.ProductCategoryId = product.Tb_PorductCategory.ProductCategoryId;
                    productEntity.ProductCategoryName = product.Tb_PorductCategory.ProductCategoryName;
                    productEntity.ProductTypeName = product.tb_ProductType.TypeName;
                    productEntity.CostTypeName = product.tb_CostType.TypeName;
                    productEntity.ProductCode = product.ProductCode;
                    productEntity.ProductDes = product.ProductDes;
                    productEntity.ProductName = product.ProductName;
                    productEntity.UnitPrice = product.UnitPrice;
                    productEntity.AppendPrice = product.AppendPrice;
                    productEntity.AreaPrice = product.AreaPrice;
                    productEntity.ProuctId = product.ProuctId;
                    productEntity.Width = product.Width;
                    productEntity.CoverHeigth = product.CoverHeight;
                    productEntity.CoverWidth = product.CoverWidth;
                    productEntity.IsRendered = product.IsRendered;
                    productEntity.IsComplex = product.IsComplex;
                    productEntity.IsPrintThumbnail = product.IsPrintThumbnail;
                    if (product.ProductPrint != null)
                    {
                        productEntity.PrintName = product.ProductPrint.PrintSizeName;
                    }
                    else
                        productEntity.PrintName = "";
                    _productEntityList.Add(productEntity);

                }
            }
            _grid.DataSource = _productEntityList;
           
        }
        /// <summary>
        /// 获得产品信息
        /// </summary>
        public void GetProductData(int rowHandle)
        {
            _productEntityList = new List<ProductEntity>();
            //List<Tb_Product> products = _bll.GetProductList();
            List<Tb_Product> products;
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                products = Entities.Tb_Product.Where(p => p.Status == 1).ToList();


                foreach (Tb_Product product in products)
                {
                    if (!product.paperFaceReference.IsLoaded)
                    {
                        product.paperFaceReference.Load();
                    }
                    if (!product.paperTypeReference.IsLoaded)
                    {
                        product.paperTypeReference.Load();
                    }
                    if (!product.printerReference.IsLoaded)
                    {
                        product.printerReference.Load();
                    }
                    if (!product.Tb_PorductCategoryReference.IsLoaded)
                    {
                        product.Tb_PorductCategoryReference.Load();
                    }
                    if (!product.tb_ProductTypeReference.IsLoaded)
                    {
                        product.tb_ProductTypeReference.Load();
                    }
                    if (!product.tb_CostTypeReference.IsLoaded)
                    {
                        product.tb_CostTypeReference.Load();
                    }
                    ProductEntity productEntity = new ProductEntity();
                    productEntity.Height = product.Height;
                    productEntity.PageNum = product.PageNum;
                    productEntity.MnemonicCode = product.mnemonicCode;
                    productEntity.PaperFaceID = product.paperFace.PaperFaceID;
                    productEntity.PaperFaceName = product.paperFace.Name;
                    productEntity.PaperTypeID = product.paperType.PaperTypeID;
                    productEntity.PaperTypeName = product.paperType.Name;
                    productEntity.PrinterName = product.printer == null ? "" : product.printer.PName;
                    productEntity.ProductCategoryId = product.Tb_PorductCategory.ProductCategoryId;
                    productEntity.ProductCategoryName = product.Tb_PorductCategory.ProductCategoryName;
                    productEntity.ProductTypeName = product.tb_ProductType.TypeName;
                    productEntity.CostTypeName = product.tb_CostType.TypeName;
                    productEntity.ProductCode = product.ProductCode;
                    productEntity.ProductDes = product.ProductDes;
                    productEntity.ProductName = product.ProductName;
                    productEntity.UnitPrice = product.UnitPrice;
                    productEntity.AppendPrice = product.AppendPrice;
                    productEntity.AreaPrice = product.AreaPrice;
                    productEntity.ProuctId = product.ProuctId;
                    productEntity.Width = product.Width;
                    productEntity.CoverHeigth = product.CoverHeight;
                    productEntity.CoverWidth = product.CoverWidth;
                    productEntity.IsRendered = product.IsRendered;
                    productEntity.IsComplex = product.IsComplex;
                    productEntity.IsPrintThumbnail = product.IsPrintThumbnail;
                    //product.IsRendered = productEntity.IsRendered;
                    //product.IsComplex = productEntity.IsComplex;
                    //product.IsPrintThumbnail = productEntity.IsPrintThumbnail;
                    _productEntityList.Add(productEntity);

                }
            }
            _grid.DataSource = _productEntityList;
            _view.FocusedRowHandle = rowHandle;
        }


        /// <summary>
        /// 获得产品信息
        /// </summary>
        public void GetProductData(Shop  obj,int rowHandle)
        {
            _productEntityList = new List<ProductEntity>();
            List<Tb_Product> products;
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                products = Entities.Tb_Product.Where(p => p.Status == 1).ToList();
                Shop shop = Entities.Shop.FirstOrDefault(s => s.ShopId == obj.ShopId);
                if (!shop.Shop_Product_Price.IsLoaded)
                {
                    shop.Shop_Product_Price.Load();
                }

                foreach (Tb_Product product in products)
                {
                    if (!product.paperFaceReference.IsLoaded)
                    {
                        product.paperFaceReference.Load();
                    }
                    if (!product.paperTypeReference.IsLoaded)
                    {
                        product.paperTypeReference.Load();
                    }
                    if (!product.Tb_PorductCategoryReference.IsLoaded)
                    {
                        product.Tb_PorductCategoryReference.Load();
                    }
                    if (!product.tb_ProductTypeReference.IsLoaded)
                    {
                        product.tb_ProductTypeReference.Load();
                    }
                    if (!product.tb_CostTypeReference.IsLoaded)
                    {
                        product.tb_CostTypeReference.Load();
                    }
                    ProductEntity productEntity = new ProductEntity();
                    productEntity.Height = product.Height;
                    productEntity.PageNum = product.PageNum;
                    productEntity.PaperFaceID = product.paperFace.PaperFaceID;
                    productEntity.PaperFaceName = product.paperFace.Name;
                    productEntity.PaperTypeID = product.paperType.PaperTypeID;
                    productEntity.PaperTypeName = product.paperType.Name;
                    productEntity.ProductCategoryId = product.Tb_PorductCategory.ProductCategoryId;
                    productEntity.ProductCategoryName = product.Tb_PorductCategory.ProductCategoryName;
                    productEntity.ProductTypeName = product.tb_ProductType.TypeName;
                    productEntity.CostTypeName = product.tb_CostType.TypeName;
                    productEntity.ProductCode = product.ProductCode;
                    productEntity.ProductDes = product.ProductDes;
                    productEntity.ProductName = product.ProductName;
                    productEntity.UnitPrice = product.UnitPrice;
                    productEntity.AppendPrice = product.AppendPrice;
                    productEntity.AreaPrice = product.AreaPrice;
                    productEntity.ProuctId = product.ProuctId;
                    productEntity.Width = product.Width;
                    productEntity.CoverHeigth = product.CoverHeight;
                    productEntity.CoverWidth = product.CoverWidth;
                    productEntity.IsRendered = product.IsRendered;
                    productEntity.IsComplex = product.IsComplex;
                    productEntity.IsPrintThumbnail = product.IsPrintThumbnail;
                    foreach (var item in shop.Shop_Product_Price)
                    {
                        if (item.ProductId == product.ProuctId)
                        {
                            productEntity.AppendPrice = Convert.ToDouble(item.AppendPrice);
                           // productEntity.AreaPrice = Convert.ToDouble(item.AreaPrice);
                            productEntity.UnitPrice = Convert.ToDouble(item.UnitPrice);
                            productEntity.IsChecked = true;
                        }
                    }
                    _productEntityList.Add(productEntity);
                }
                _grid.DataSource = _productEntityList;
                _view.FocusedRowHandle = rowHandle;
            }
        }

        /// <summary>
        /// 获得委外产品信息
        /// </summary>
        public void GetProductData(OutProcessShop obj)
        {
            _productEntityList = new List<ProductEntity>();
            //List<Tb_Product> products = _bll.GetProductList();
            List<Tb_Product> products;
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {

                products = Entities.Tb_Product.Where(p => p.Status == 1).ToList();

                OutProcessShop outShop = Entities.OutProcessShop.FirstOrDefault(o => o.OutShopId == obj.OutShopId);

                if (!outShop.Tb_OutShop_ProductCategory.IsLoaded)
                {
                    outShop.Tb_OutShop_ProductCategory.Load();
                }
                foreach (Tb_Product product in products)
                {
                    if (!product.paperFaceReference.IsLoaded)
                    {
                        product.paperFaceReference.Load();
                    }
                    if (!product.paperTypeReference.IsLoaded)
                    {
                        product.paperTypeReference.Load();
                    }
                    if (!product.Tb_PorductCategoryReference.IsLoaded)
                    {
                        product.Tb_PorductCategoryReference.Load();
                    }
                    if (!product.tb_ProductTypeReference.IsLoaded)
                    {
                        product.tb_ProductTypeReference.Load();
                    }
                    if (!product.tb_CostTypeReference.IsLoaded)
                    {
                        product.tb_CostTypeReference.Load();
                    }
                    ProductEntity productEntity = new ProductEntity();
                    productEntity.Height = product.Height;
                    productEntity.PageNum = product.PageNum;
                    productEntity.PaperFaceID = product.paperFace.PaperFaceID;
                    productEntity.PaperFaceName = product.paperFace.Name;
                    productEntity.PaperTypeID = product.paperType.PaperTypeID;
                    productEntity.PaperTypeName = product.paperType.Name;
                    productEntity.ProductCategoryId = product.Tb_PorductCategory.ProductCategoryId;
                    productEntity.ProductCategoryName = product.Tb_PorductCategory.ProductCategoryName;
                    productEntity.ProductCode = product.ProductCode;
                    productEntity.ProductDes = product.ProductDes;
                    productEntity.ProductName = product.ProductName;
                    productEntity.UnitPrice = 0;
                    //productEntity.AppendPrice = product.AppendPrice;
                    //productEntity.AreaPrice = product.AreaPrice;
                    productEntity.ProuctId = product.ProuctId;
                    productEntity.Width = product.Width;
                    productEntity.CoverHeigth = product.CoverHeight;
                    productEntity.CoverWidth = product.CoverWidth;
                    productEntity.IsRendered = product.IsRendered;
                    productEntity.IsComplex = product.IsComplex;
                    foreach (var item in outShop.Tb_OutShop_ProductCategory)
                    {
                        if (item.ProductId == product.ProuctId)
                        {
                            productEntity.NeedTime = item.NeedTime;
                            productEntity.IsChecked = true;
                            productEntity.UnitPrice =(double)item.UnitPrice;
                            productEntity.IsProductDefaultShop = item.IsDefaultOutShop;
                        }

                    }
                    _productEntityList.Add(productEntity);
                }
                _grid.DataSource = _productEntityList;
            }

        }
 
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="flowTemController"></param>
        public void AddProduct(ProductEntity obj, FlowTemplateController flowTemController,int outShopid)
        {
            
            Tb_Product product = new Tb_Product();
            PaperFaceBLL pFaceBLL = new PaperFaceBLL();
            PaperTypeBLL pTypeBLL = new PaperTypeBLL();
            ProductCategoryBLL productCategoryBLL = new ProductCategoryBLL();
            ProductTypeBLL productTypeBLL = new ProductTypeBLL();
            CostTypeBLL costTypeBLL = new CostTypeBLL();
            PrinterBLL printerBLL = new PrinterBLL();
            printer defaultPrinter = printerBLL.GetPrinterObj(obj.PrinterName);
            paperFace pFace = pFaceBLL.GetPaperFaceObj(obj.PaperFaceName);
            paperType pType = pTypeBLL.GetPaperTypeObj(obj.PaperTypeName);
            Tb_PorductCategory productCategory = productCategoryBLL.GetProductCategoryObj(obj.ProductCategoryName);
            tb_ProductType productType = productTypeBLL.GetProductTypeObj(obj.ProductTypeName);
            tb_CostType costType = costTypeBLL.GetCostTypeObj(obj.CostTypeName); 
            //List<FlowTemplateEntity> flowTemplateEntityList = flowTemController.FlowTemplateEntityList;
            List<FlowTemplateEntity> flowTemplateEntityList = new List<FlowTemplateEntity>();
            for (int i = 0; i < flowTemController.View.DataRowCount; i++)
            {
                flowTemplateEntityList.Add((FlowTemplateEntity)flowTemController.View.GetRow(i));
            }

            foreach (FlowTemplateEntity flowTemplateEntity in flowTemplateEntityList)
            {
                TechBLL techBLL = new TechBLL();
                tb_Tech tech = techBLL.GetTechObj(flowTemplateEntity.TechName);
                Tb_FlowTemplate flowTemplate = new Tb_FlowTemplate();
                flowTemplate.IsNecessery = flowTemplateEntity.IsNecessery;
                flowTemplate.WorkOrder = flowTemplateEntity.WorkOrder;
                flowTemplate.tb_Tech = tech;
                product.Tb_FlowTemplate.Add(flowTemplate);
            }
            product.paperFace = pFace;
            product.paperType = pType;
            product.printer = defaultPrinter;
            product.Tb_PorductCategory = productCategory;
            product.tb_ProductType = productType;
            product.tb_CostType = costType;
            product.Height = obj.Height;
            product.PageNum = obj.PageNum;
            product.ProductCode = obj.ProductCode;
            product.ProductDes = obj.ProductDes;
            product.ProductName = obj.ProductName;
            product.Status = (int)ProductStatus.Normal;
            product.Width = obj.Width;
            product.AppendPrice = obj.AppendPrice;
            product.UnitPrice = obj.UnitPrice;
            product.mnemonicCode = obj.MnemonicCode;
            product.CoverHeight = obj.CoverHeigth;
            product.CoverWidth = obj.CoverWidth;
            product.IsRendered = obj.IsRendered;
            product.IsComplex = obj.IsComplex;
            product.IsPrintThumbnail = obj.IsPrintThumbnail;
            if (string.IsNullOrEmpty(obj.PrintName))
            {
                _bll.AddProduct(product);
            }
            else
            {
                _bll.AddProduct(product,obj.PrintName);
            }
            if (outShopid != 0)
            {
                _bll.AddOutShopCategory(product.ProuctId, outShopid);
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="flowTemController"></param>
        public void UpdateProduct(ProductEntity obj, FlowTemplateController flowTemController)
        {
            List<Tb_FlowTemplate> flowTemplateList = new List<Tb_FlowTemplate>();
            List<Tb_FlowTemplate> flowTemplateListDelete = new List<Tb_FlowTemplate>();
            Tb_Product product = _bll.GetProductObj(obj.ProuctId);
            //如果默认价格改变，则修改产品设定价格
            if (product.AppendPrice != obj.AppendPrice || product.UnitPrice != obj.UnitPrice)
            {
                if (!product.Shop_Product_Price.IsLoaded)
                {
                    product.Shop_Product_Price.Load();
                }
                foreach (var item in product.Shop_Product_Price)
                {
                    //item.AppendPrice = obj.AppendPrice;
                    //item.UnitPrice = obj.UnitPrice;
                }
            }
            PaperFaceBLL pFaceBLL = new PaperFaceBLL();
            PaperTypeBLL pTypeBLL = new PaperTypeBLL();
            PrinterBLL printerBLL = new PrinterBLL();
            FlowTemplateBLL flowTemplateBLL = new FlowTemplateBLL();
            foreach (Tb_FlowTemplate flowtemobj in product.Tb_FlowTemplate)
            {
                flowTemplateListDelete.Add(flowtemobj);
            }
            foreach (Tb_FlowTemplate item in flowTemplateListDelete)
            {
                flowTemplateBLL.DeleteFlowTemplate(item);
            }
            ProductCategoryBLL productCategoryBLL = new ProductCategoryBLL();
            ProductTypeBLL productTypeBLL = new ProductTypeBLL();

            paperFace pFace = pFaceBLL.GetPaperFaceObj(obj.PaperFaceName);
            paperType pType = pTypeBLL.GetPaperTypeObj(obj.PaperTypeName);
            printer defaultPrinter = printerBLL.GetPrinterObj(obj.PrinterName);
            CostTypeBLL costTypeBLL = new CostTypeBLL();
            Tb_PorductCategory productCategory = productCategoryBLL.GetProductCategoryObj(obj.ProductCategoryName);
            tb_ProductType productType = productTypeBLL.GetProductTypeObj(obj.ProductTypeName);
            tb_CostType costType = costTypeBLL.GetCostTypeObj(obj.CostTypeName); 

            //List<FlowTemplateEntity> flowTemplateEntityList = flowTemController.FlowTemplateEntityList;
            List<FlowTemplateEntity> flowTemplateEntityList = new List<FlowTemplateEntity>();
            for(int i= 0;i<flowTemController.View.RowCount;i++)
            {
                flowTemplateEntityList.Add((FlowTemplateEntity)flowTemController.View.GetRow(i));
            }

            foreach (FlowTemplateEntity flowTemplateEntity in flowTemplateEntityList)
            {
                TechBLL techBLL = new TechBLL();
                tb_Tech tech = techBLL.GetTechObj(flowTemplateEntity.TechName);

                Tb_FlowTemplate flowTemplate = new Tb_FlowTemplate();
                flowTemplate.IsNecessery = flowTemplateEntity.IsNecessery;
                flowTemplate.WorkOrder = flowTemplateEntity.WorkOrder;
                flowTemplate.tb_Tech = tech;
                product.Tb_FlowTemplate.Add(flowTemplate);
                flowTemplateList.Add(flowTemplate);
            }

            product.paperFace = pFace;
            product.paperType = pType;
            product.printer = defaultPrinter;
            product.Tb_PorductCategory = productCategory;
            product.tb_ProductType = productType;
            product.tb_CostType = costType;
            product.Height = obj.Height;
            product.PageNum = obj.PageNum;
            product.ProductCode = obj.ProductCode;
            product.ProductDes = obj.ProductDes;
            product.ProductName = obj.ProductName;
            product.Width = obj.Width;
            product.UnitPrice = obj.UnitPrice;
            product.AppendPrice = obj.AppendPrice;
            product.mnemonicCode = obj.MnemonicCode;
            product.CoverHeight = obj.CoverHeigth;
            product.CoverWidth = obj.CoverWidth;
            product.IsRendered = obj.IsRendered;
            product.IsComplex = obj.IsComplex;
            product.IsPrintThumbnail = obj.IsPrintThumbnail;
            if (string.IsNullOrEmpty(obj.PrintName))
            {
                _bll.UpdateProduct(product);
            }
            else
            {
                product.ProductPrintReference.Load();
                if(product.ProductPrint!=null)
                {
                    product.ProductPrint.PrintSizeName = obj.PrintName;
                    _bll.UpdateProduct(product);
                }
                else
                {
                    ProductPrint pp=new ProductPrint();
                    pp.Productid = product.ProuctId;
                    pp.PrintSizeName = obj.PrintName;
                    pp.Tb_Product = product;
                    _bll.UpdateProduct(product,pp);
                }
            }
            
            
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="obj"></param>
        public void DeleteProduct(ProductEntity obj)
        {
            _bll.DeleteProduct(obj);
        }
        /// <summary>
        /// 绑定纸面
        /// </summary>
        public void LinkcmbPaperFace()
        {
            PaperFaceBLL bll = new PaperFaceBLL();
            List<paperFace> list = bll.GetPaperFaceList();
            foreach (paperFace obj in list)
            {
                _comboBox.Properties.Items.Add(obj.Name);
            }
        }
        public void LinkDefaultPrinter()
        {
            PrinterBLL bll = new PrinterBLL();
            List<PrinterInfo> list = bll.GetPrinterList();
            foreach (PrinterInfo obj in list)
            {
                _comboBox.Properties.Items.Add(obj.PName);
            }
        }
        /// <summary>
        /// 绑定纸型
        /// </summary>
        public void LinkcmbPaperType()
        {
            PaperTypeBLL bll = new PaperTypeBLL();
            List<paperType> list = bll.GetPaperTypeList();
            foreach(paperType obj in list)
            {
                _comboBox.Properties.Items.Add(obj.Name);
            }
            
        }
        /// <summary>
        /// 绑定产品分类
        /// </summary>
        public void LinkcmbPorductCategory()
        {
            ProductCategoryBLL bll = new ProductCategoryBLL();
            List<Tb_PorductCategory> list = bll.GetProductCategoryList();
            foreach(Tb_PorductCategory obj in list)
            {
                _comboBox.Properties.Items.Add(obj.ProductCategoryName);
            }
        }

        /// <summary>
        /// 绑定产品类型
        /// </summary>
        public void LinkcmbPorductType()
        {
            ProductTypeBLL bll = new ProductTypeBLL();
            List<tb_ProductType> list = bll.GetProductTypeList();
            foreach (tb_ProductType obj in list)
            {
                _comboBox.Properties.Items.Add(obj.TypeName);
            }
        }

        /// <summary>
        /// 绑定计价类型
        /// </summary>
        public void LinkcmbCosType()
        {
            CostTypeBLL bll = new CostTypeBLL();
            List<tb_CostType> list = bll.GetCostTypeList();
            foreach (tb_CostType obj in list)
            {
                _comboBox.Properties.Items.Add(obj.TypeName);
            }
        }

        /// <summary>
        /// 获取商户没选定的产品
        /// </summary>
        /// <returns></returns>
        public List<ProductEntity> GetNoCheckShopProduct()
        {
            //List<ProductEntity> ProductList = new List<ProductEntity>();
            //for (int i = 0; i < _view.DataRowCount; i++)
            //{
            //    ProductEntity product = (ProductEntity)_view.GetRow(i);
            //    if (product != null)
            //    {
            //        if (!product.IsChecked)
            //        {
            //            ProductList.Add(product);
            //        }
            //    }
            //}
            //return ProductList;
            List<ProductEntity> temlist = (List<ProductEntity>)_view.DataSource;
            return temlist.Where(t => t.IsChecked == false).ToList();
        }
        
        /// <summary>
        /// 获得委外厂商能加工的产品列表
        /// </summary>
        /// <returns></returns>
        public List<ProductEntity> GetOutProcessShopProduct()
        {
            //List<ProductEntity> ProductList = new List<ProductEntity>();

            //for (int i = 0; i < _view.RowCount; i++)
            //{

            //    ProductEntity product = (ProductEntity)_view.GetRow(i);
            //    if (product.IsChecked)
            //    {
            //        ProductList.Add(product);
            //    }
            //}
            //return ProductList; 
            List<ProductEntity> temlist = (List<ProductEntity>)_view.DataSource;
            return temlist.Where(t => t.IsChecked == true).ToList();
        }
        /// <summary>
        /// 加价
        /// </summary>
        /// <param name="productEntity"></param>
        /// <param name="addPagePrice"></param>
        /// <param name="unitPrice"></param>
        public void AddPrice(ProductEntity productEntity,double addPagePrice,double unitPrice)
        {
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                Tb_Product product = Entities.Tb_Product.FirstOrDefault(p =>p.ProuctId ==productEntity.ProuctId);
                if (product != null)
                {
                    product.AppendPrice += addPagePrice;
                    product.UnitPrice += unitPrice;
                    if (!product.Shop_Product_Price.IsLoaded)
                    {
                        product.Shop_Product_Price.Load();

                    }
                    foreach (var item in product.Shop_Product_Price)
                    {
                        //item.AppendPrice += addPagePrice;
                        //item.UnitPrice += unitPrice;
                    }
                    Entities.SaveChanges();
                }
            }
        }

        /// <summary>
        /// 减价
        /// </summary>
        /// <param name="productEntity"></param>
        /// <param name="addPagePrice"></param>
        /// <param name="unitPrice"></param>
        public void ReducePrice(ProductEntity productEntity, double addPagePrice, double unitPrice)
        {
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                Tb_Product product = Entities.Tb_Product.FirstOrDefault(p => p.ProuctId == productEntity.ProuctId);
                if (product != null)
                {
                    product.AppendPrice -= addPagePrice;
                    product.UnitPrice -= unitPrice;
                    if (!product.Shop_Product_Price.IsLoaded)
                    {
                        product.Shop_Product_Price.Load();

                    }
                    foreach (var item in product.Shop_Product_Price)
                    {
                        //item.AppendPrice -= addPagePrice;
                        //item.UnitPrice -= unitPrice;
                    }
                    Entities.SaveChanges();
                }
            }
        }

        /// <summary>
        /// 获得产品默认委外商
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public OutProcessShop GetProductDefaultOutShop(int productId)
        {
            ProductOutShopBLL posbll = new ProductOutShopBLL();
            return posbll.GetProductDefaultShop(productId);
        }

        public string GetProductPrintName(int inProductID)
        {
            using (newSampleYPDBEntities db=new newSampleYPDBEntities())
            {
                ProductPrint pp = db.ProductPrint.FirstOrDefault(p => p.Productid == inProductID);
                if (pp!=null)
                {
                    return pp.PrintSizeName;
                }
                else
                {
                    return "";
                }
            }
        }

        #endregion
    }
}
